package vip.xiaonuo.biz.modular.lancerGoods.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.lancerGoods.service.BizGoodsService;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoods;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDetailDTO;
import vip.xiaonuo.biz.modular.pojo.dto.page.GoodsPageDTO;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "消费者商品管理")
@ApiSupport(author = "LANCER_TEAM", order = 1)
@RestController
@RequestMapping
@Validated
public class ClientGoodsController {
    @Autowired
    private BizGoodsService GoodsService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取商品盒子下的所有商品")
    @GetMapping("/biz/client/goods/getAllGoodsByGoodsBox")
    public CommonResult<List<BizGoods>> getAllGoodsByGoodsBoxId(@Valid GoodsPageDTO dto) {
        List<BizGoods> res = GoodsService.getAllGoodsByGoodsBox(dto);
        return CommonResult.data(res);
    }
    @ApiOperationSupport(order = 2)
    @ApiOperation("商品详情")
    @GetMapping("/biz/client/goods/detail")
    public CommonResult<BizGoods> detail(@Valid CommonDetailDTO dto) {
        BizGoods res = GoodsService.detail(dto);
        return CommonResult.data(res);
    }

//    @ApiOperationSupport(order = 3)
//    @ApiOperation("商品购买")
//    @GetMapping("/buyGoods/{id}/{count}")
//    public CommonResult<String> buyGoods(@PathVariable String id, @PathVariable Integer count) {
//        GoodsService.buyGoods(id,count);
//        return CommonResult.ok();
//    }
    @ApiOperationSupport(order = 3)
    @ApiOperation("商品购买支付宝通过商品id")
    @GetMapping("/biz/client/goods/buyGoodsByAliPay/{id}/{count}")
    public CommonResult<String> buyGoodsByAliPay(@PathVariable String id, @PathVariable Integer count,
                                                 HttpServletResponse httpResponse) {
        GoodsService.buyGoodsByAliPay(id,count,httpResponse);
        return CommonResult.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation("商品购买支付宝通过订单号")
    @GetMapping("/biz/client/goods/buyGoodsByAliPay/orderId/{orderId}")
    public CommonResult<String> buyGoodsByAliPay(@PathVariable String orderId,
                                                 HttpServletResponse httpResponse) {
        GoodsService.buyGoodsByAliPayAndOrderId(orderId,httpResponse);
        return CommonResult.ok();
    }
    // 配置购买商品的回调函数
    /**
     * 支付成功的回调 这个回调接口一般使用在支付完成后需要执行的业务比
     *  如 删除该订单 商品数减去相应的数量等等
     * */
    @PostMapping(value = "/biz/client/goods/pay/fallback")
    public void payFallback (HttpServletRequest request) {
        GoodsService.payBack(request);
    }
}
